//
//  PopTips.m
//  popViewDemo
//
//  Created by warlee on 13-1-22.
//

#import "PopTips.h"
@implementation PopTips

#define OffsetYPer      (0.8)

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {        
        // Initialization code
        isLoadingViewDisplay = NO;
    }
    return self;
}

+(void)tips:(UIView *)superView Msg:(NSString *)msg{
    [PopTips tips:superView Msg:msg
         FontSize:14 TipsAt:PopTipsAtBottom
        TipsStyle:PopTipsStyleConner
    TipsBackColor:[UIColor blackColor]
    tipsTextColor:[UIColor whiteColor]];
}

+(void)tipsAtMiddle:(UIView *)superView Msg:(NSString *)msg{
    [PopTips tips:superView Msg:msg
         FontSize:14 TipsAt:PopTipsAtMiddle
        TipsStyle:PopTipsStyleConner
    TipsBackColor:[UIColor blackColor]
    tipsTextColor:[UIColor whiteColor]];
}

+(void)tipsAtTop:(UIView *)superView Msg:(NSString *)msg{
    [PopTips tips:superView Msg:msg
         FontSize:14 TipsAt:PopTipsAtTop
        TipsStyle:PopTipsStyleConner
    TipsBackColor:[UIColor blackColor]
    tipsTextColor:[UIColor whiteColor]];
}

+(void)tips:(UIView *)superView Msg:(NSString *)msg FontSize:(int)fontSize{
    [PopTips tips:superView Msg:msg
         FontSize:fontSize TipsAt:PopTipsAtBottom
        TipsStyle:PopTipsStyleConner
    TipsBackColor:[UIColor blackColor]
    tipsTextColor:[UIColor whiteColor]];
}
+(void)tips:(UIView *)superView Msg:(NSString *)msg FontSize:(int)fontSize
    TipsAt:(PopTipsAt)tipsAt TipsStyle:(PopTipsStyle)tipsStyle
    TipsBackColor:(UIColor *)tipsBackColor tipsTextColor:(UIColor *)tipsTextColor{

    CGRect mainFrame = superView.bounds;
    UIFont *tipsFont= [UIFont fontWithName:@"helvetica" size:fontSize];
    int textWidth = [msg sizeWithFont:tipsFont].width;  //文字的宽度
    int emWidth   = textWidth / [msg length];           //一个文字的宽度
    CGFloat alpha = POPTIPS_ALPHA;
    
    CGRect frame  = CGRectMake(0, 0, textWidth + emWidth*3, emWidth*2.5);
    frame.origin.x= (mainFrame.size.width - frame.size.width) / 2;    
    if(tipsAt == PopTipsAtMiddle){
        frame.origin.y= (mainFrame.size.height - frame.size.height) /2;
    }else if(tipsAt == PopTipsAtBottom){
        frame.origin.y= (mainFrame.size.height - frame.size.height) *OffsetYPer;
    }else{
        frame.origin.y= (mainFrame.size.height - frame.size.height) *(1-OffsetYPer);
    }

    //添加阴影
    UIView * shadow = [[UIView alloc] initWithFrame:frame];
    shadow.BackgroundColor = tipsBackColor;
    if (tipsStyle == PopTipsStyleConner) {
        shadow.layer.cornerRadius = emWidth/1.5;
    }else{
        shadow.layer.masksToBounds  = NO; //mask作为bound的边界，对原来的frame进行裁剪
        shadow.layer.shadowOffset   = CGSizeMake(1, 1);
        //shadow.layer.shadowOffset   = CGSizeMake(-10, 15);
        shadow.layer.shadowRadius   = emWidth/2;
        shadow.layer.shadowColor    = tipsBackColor.CGColor;
        shadow.layer.shadowOpacity  = 1;
    }
    
    UILabel * tips = [[UILabel alloc] initWithFrame:frame];
    tips.textColor = tipsTextColor;
    tips.backgroundColor = [UIColor clearColor];
    tips.textAlignment = NSTextAlignmentCenter;
    tips.font = tipsFont;
    tips.text = msg;
    tips.alpha = 0;
    
    [superView addSubview:shadow];
    [superView addSubview:tips];
    tips.transform = CGAffineTransformMakeScale(0.4, 0.4);
    shadow.transform = tips.transform;
    [UIView animateWithDuration:0.2 animations:^{
        tips.alpha = 1;        
        tips.transform = CGAffineTransformMakeScale(1.2, 1.2);
        shadow.alpha = alpha;
        shadow.transform = tips.transform;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            tips.transform = CGAffineTransformMakeScale(1, 1);
            shadow.transform = tips.transform;
        }completion:^(BOOL finished){
            [UIView animateWithDuration:.5 delay:1.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                tips.alpha = 0;
                shadow.alpha = 0;
            }completion:^(BOOL finished){
                [tips removeFromSuperview];
                [shadow removeFromSuperview];
            }];
        }];
    }];
}





-(void)loadingAdd:(UIView *)superView{
    if (!isLoadingViewDisplay) {
        [self _initWaitView:superView];
        loadingView.alpha=0;
        [superView addSubview:loadingView];
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha=0.7;
        }];
        isLoadingViewDisplay = YES;
    }
}
-(void)loadingClose{
    if (isLoadingViewDisplay) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha=0;
        }completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
        }];
        isLoadingViewDisplay = NO;
    }
}
-(void)_initWaitView:(UIView *)superView{
    int width = 100;
    int height = 100;
    CGRect mainFrame = superView.bounds;
    int offset_x = (mainFrame.size.width - width)/2;
    int offset_y = (mainFrame.size.height - height)/2;
    
    loadingView = [[UIView alloc] initWithFrame:CGRectMake(offset_x,offset_y, width, height)];
    [loadingView setBackgroundColor:[UIColor blackColor]];
    loadingView.layer.cornerRadius = 5;
    
    
    UIActivityIndicatorView * loading = [[UIActivityIndicatorView alloc]
                                         initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    CGRect loadingFrame = loading.frame;
    loadingFrame.origin.x = (width-loadingFrame.size.width)/2;
    loadingFrame.origin.y =(height-loadingFrame.size.height)/2;
    [loading setFrame:loadingFrame];
    [loading startAnimating];
    [loadingView addSubview:loading];
    loadingView.alpha = 0;
    
    if (!isLoadingViewDisplay) {        
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha=0.7;
        }];
        isLoadingViewDisplay = YES;
    }
}

@end
